package com.project.weimi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.project.weimi.R;
import com.project.weimi.adapter.MyFragmentPagerAdapter;
import com.project.weimi.entity.response.OrderEntity;
import com.project.weimi.fragment.AllOrder_Fragment;
import com.project.weimi.fragment.Inbound_Fragment;
import com.project.weimi.fragment.Pendingpayment_Fragment;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class MyOrderActivity extends BaseActivity {
	private static final String TAG = "TAG";
	private TextView all_order, pending_payment, inbound;
	private ViewPager mPager;
	private ImageView ivBottomLine;
	private int currIndex = 0;
	private int bottomLineWidth;
	private int offset = 0;
	private int position_one;
	private int position_two;
	private int position_three;
	private Resources resources;

	private ArrayList<Fragment> fragmentsList;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_myorder);
		resources = getResources();
		InitWidth();
		InitTextView();
		InitViewPager();
	}

	private void InitWidth() {
		ivBottomLine = (ImageView) findViewById(R.id.iv_bottom_line);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		LayoutParams para = (LayoutParams) ivBottomLine.getLayoutParams();

		para.height = para.height;
		para.width = (int) (dm.widthPixels / 5f);
		ivBottomLine.setLayoutParams(para);

		bottomLineWidth = ivBottomLine.getLayoutParams().width;
		Log.d(TAG, "cursor imageview width=" + bottomLineWidth);
		int screenW = dm.widthPixels;
		// int screenW = 265;
		offset = (int) ((screenW / 5.0 - bottomLineWidth) / 2);
		Log.i(TAG, "offset=" + offset);

		position_one = (int) (screenW / 5.0);
		position_two = position_one * 2;
		position_three = position_one * 3;
	}

	private void InitTextView() {
		all_order = (TextView) findViewById(R.id.all_order);
		pending_payment = (TextView) findViewById(R.id.pending_payment);
		inbound = (TextView) findViewById(R.id.inbound);
		all_order.setOnClickListener(new MyOnClickListener(0));
		pending_payment.setOnClickListener(new MyOnClickListener(1));
		inbound.setOnClickListener(new MyOnClickListener(2));
	}

	private void InitViewPager() {
		mPager = (ViewPager) findViewById(R.id.vPager);
		fragmentsList = new ArrayList<Fragment>();
		AllOrder_Fragment order_Fragment = new AllOrder_Fragment();
		Pendingpayment_Fragment pendingpayment_Fragment = new Pendingpayment_Fragment();
		Inbound_Fragment inbound_Fragment = new Inbound_Fragment();

		fragmentsList.add(order_Fragment);
		fragmentsList.add(pendingpayment_Fragment);
		fragmentsList.add(inbound_Fragment);

		mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentsList));
		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	@Override
	void setStatusBar(int statusBarColorRes) {
		// TODO Auto-generated method stub
		super.setStatusBar(R.color.transparent_3);
	}

	public class MyOnClickListener implements View.OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			mPager.setCurrentItem(index);
		}
	};

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageSelected(int arg0) {
			Animation animation = null;
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					animation = new TranslateAnimation(position_one, 0, 0, 0);
					pending_payment.setTextColor(resources.getColor(R.color.white));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two, 0, 0, 0);
					inbound.setTextColor(resources.getColor(R.color.white));
				}
				all_order.setTextColor(resources.getColor(R.color.purple));
				break;
			case 1:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_one, 0, 0);
					all_order.setTextColor(resources.getColor(R.color.white));
				} else if (currIndex == 2) {
					animation = new TranslateAnimation(position_two, position_one, 0, 0);
					inbound.setTextColor(resources.getColor(R.color.white));
				}
				pending_payment.setTextColor(resources.getColor(R.color.purple));
				break;
			case 2:
				if (currIndex == 0) {
					animation = new TranslateAnimation(0, position_two, 0, 0);
					all_order.setTextColor(resources.getColor(R.color.white));
				} else if (currIndex == 1) {
					animation = new TranslateAnimation(position_one, position_two, 0, 0);
					pending_payment.setTextColor(resources.getColor(R.color.white));
				}
				inbound.setTextColor(resources.getColor(R.color.purple));
				break;

			}
			currIndex = arg0;
			animation.setFillAfter(true);
			animation.setDuration(300);
			ivBottomLine.startAnimation(animation);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

}
